package com.fruit.dao.impl;

import com.fruit.dao.BaseDAO;
import com.fruit.dao.FruitDAO;
import com.fruit.pojo.Fruit;
import com.fruit.pojo.FruitCategory;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FruitDAOImpl implements FruitDAO {
    private BaseDAO baseDAO = new BaseDAO();

    @Override
    public void addFruit(Fruit fruit) throws Exception {
        try {
            String sql = "insert into FRUIT(CATEGORY_ID,NAME,PLACE,WEIGHT,PRICE) values(?,?,?,?,?)";
            List<Object> params = new ArrayList<>();
            params.add(fruit.getFruitCategory().getId());
            params.add(fruit.getName());
            params.add(fruit.getPlace());
            params.add(fruit.getWeight());
            params.add(fruit.getPrice());
            baseDAO.excuteUpdate(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baseDAO.closeAll();
        }
    }

    @Override
    public List<Fruit> getFruitList(Fruit criteria) throws Exception {
        try {
            String sql = "select F.ID,F.NAME,F.WEIGHT,F.PRICE,F.PLACE,c.NAME CATEGORY_NAME from FRUIT F inner join FRUIT_CATEGORY c  on c.id=F.category_id where 1=1";
            List<Object> params = new ArrayList<>();
            if (criteria.getId() != null) {
                sql += " and F.ID=? ";
                params.add(criteria.getId());
            }
            if (criteria.getName() != null && criteria.getName().trim().length() > 0) {
                sql += " and F.NAME like ? ";
                params.add("%" + criteria.getName() + "%");
            }
            if (criteria.getFruitCategory() != null && criteria.getFruitCategory().getId() != null) {
                sql += " and C.ID = ? ";
                params.add(criteria.getFruitCategory().getId());
            }
            ResultSet resultSet = baseDAO.excuteQuery(sql, params);
            List<Fruit> fruitList = new ArrayList<>();
            while (resultSet.next()) {
                Fruit fruit = new Fruit();
                fruit.setWeight(resultSet.getInt("WEIGHT"));
                fruit.setPrice(resultSet.getInt("PRICE"));
                fruit.setName(resultSet.getString("NAME"));
                fruit.setPlace(resultSet.getString("PLACE"));
                fruit.setId(resultSet.getInt("ID"));
                FruitCategory category = new FruitCategory();
                category.setName(resultSet.getString("CATEGORY_NAME"));
                fruit.setFruitCategory(category);
                fruitList.add(fruit);
            }
            return fruitList;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            baseDAO.closeAll();
        }
    }

    @Override
    public void deleteFruit(Integer id) throws Exception {
        try {
            String sql = "delete  from FRUIT where ID=?";
            List<Object> params = new ArrayList<>();
            params.add(id);
            baseDAO.excuteUpdate(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            baseDAO.closeAll();
        }
    }

    @Override
    public Fruit getFruitById(Integer id) throws Exception {
        try {
            String sql = "select F.ID,F.NAME,F.WEIGHT,F.PRICE,F.PLACE,c.NAME CATEGORY_NAME from FRUIT F inner join FRUIT_CATEGORY c  on c.id=F.category_id where F.ID=?";
            List<Object> params = new ArrayList<>();
            params.add(id);
            ResultSet resultSet = baseDAO.excuteQuery(sql, params);
            if (resultSet.next()) {
                Fruit fruit = new Fruit();
                fruit.setWeight(resultSet.getInt("WEIGHT"));
                fruit.setPrice(resultSet.getInt("PRICE"));
                fruit.setName(resultSet.getString("NAME"));
                fruit.setPlace(resultSet.getString("PLACE"));
                fruit.setId(resultSet.getInt("ID"));
                FruitCategory category = new FruitCategory();
                category.setName(resultSet.getString("CATEGORY_NAME"));
                fruit.setFruitCategory(category);
                return fruit;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            baseDAO.closeAll();
        }
    }
}
